#ifndef ENCODING_H
#define ENCODING_H
void cal_nvarbits(){
	double a;
/*	a=(upper_bound-lower_bound)*(double)1/precision; */
	a=(upper_bound-lower_bound)*pow(10,precision_digits);
	nvarbits= ((int) floor(log(a)/log(2)))+1;
}
double gray2dec_range(char* gray){
	int i,c;
	double r=0.0;
	int last=0;
	for(i=nvarbits-1;i>=0;i--){
		c=gray[i]+last;
		if (c==2) c=0; else r+=(c*power_two[i]);
		last=c;
	}
	r=(upper_bound-lower_bound)*(r/(power_two[nvarbits]-1))+lower_bound;
/*	return trunc(r,precision_digits);*/
	return r;
}
void vector_gray2dec_range(char* bit_stream,double* x){
	int i;
	int n=0;
	for(i=0;i<dimension;i++){
		x[i]=gray2dec_range(bit_stream+n);
		n+=nvarbits;
	}
}
double bin2dec_range(char* bit_stream){
	int i=0;
	double r=0.0;
	for(i=0;i<nvarbits;i++)
		r+=(bit_stream[i]*power_two[i]);
	r=(upper_bound-lower_bound)*(r/(power_two[nvarbits]-1))+lower_bound;
/*	return trunc(r,precision_digits);*/
	return r;
}
void vector_bin2dec_range(char* bit_stream,double* x){
	int i;
	int n=0;
	for(i=0;i<dimension;i++){
		x[i]=bin2dec_range(bit_stream+n);
		n+=nvarbits;
	}
}

#endif
